package cs275.ants.graph.antgraph;

import junit.framework.TestCase;

import org.easymock.EasyMock;
import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.replay;
import static org.easymock.EasyMock.verify;

public class AntGraphEdgeTest extends TestCase {
	private AntGraphEdge edge;
	private AntGraphVertex node1;
	private AntGraphVertex node2;
	
	
	@Override
	protected void setUp() throws Exception {
		node1 = EasyMock.createNiceMock(AntGraphVertex.class);
		node2 = EasyMock.createNiceMock(AntGraphVertex.class);
		edge = new AntGraphEdge(node1, node2);
	}
	
	public void testUpdatePheromone() {
		expect(node1.hasFood()).andStubReturn(false);
		expect(node2.hasFood()).andStubReturn(false);
		
		replay(node1, node2);
		
		int initialCost = 5;
		edge.setCost(initialCost);
		edge.updatePheromone();
		double expectedCost = initialCost * (1-AntGraphEdge.EVAPORATION_COEFFICIENT);
		assertEquals(expectedCost, edge.getCost());
		
		double previousCost;
		for (int i = 0; i < 10; i++) {
			previousCost = edge.getCost();
			edge.updatePheromone();
			assertTrue(previousCost > edge.getCost());
			assertTrue(initialCost > edge.getCost());
		}
		verify(node1, node2);
	}
	
	public void testUpdatePheromone_afterDeposit() {
		int initialCost = 5;
		edge.setCost(initialCost);
		edge.depositPheromone(1);
		edge.updatePheromone();
		double expectedCost = initialCost * (1 - AntGraphEdge.EVAPORATION_COEFFICIENT) + 1;
		assertEquals(expectedCost, edge.getCost());
	}
	
	public void testUpdatePheromone_edgeHasFood() {
		expect(node1.hasFood()).andStubReturn(true);
		expect(node2.hasFood()).andStubReturn(false);
		
		replay(node1, node2);
		edge.setCost(5);
		assertEquals(5, edge.getCost(), 0);
		edge.updatePheromone();
		assertEquals(Double.MAX_VALUE, edge.getCost(), 0);
	}

	public void testDepositPheromone() {
		edge.depositPheromone(1);
		assertEquals(1.0, edge.getCost());
		assertEquals(1.0, edge.updateQuantity);
		edge.updatePheromone();
		assertEquals(2.0, edge.getCost());
		assertEquals(0.0, edge.updateQuantity);
	}
}
